package com.job.updateinfojob.util;


import com.job.updateinfojob.error.ErrorCode;
import com.job.updateinfojob.res.R;
import org.springframework.http.HttpStatus;

public class ResultUtil<T> {
    private int successCode = 200;
    private R<T> r = new R();

    private ResultUtil() {
        this.r.setSuccess(true);
        this.r.setMessage("成功");
        this.r.setCode(this.successCode);
    }

    private R<T> setData(T t) {
        this.r.setData(t);
        this.r.setCode(this.successCode);
        return this.r;
    }

    private R<T> setSuccessMsg(String msg) {
        this.r.setSuccess(true);
        this.r.setMessage(msg);
        this.r.setCode(this.successCode);
        this.r.setData((Object)null);
        return this.r;
    }

    private R<T> setData(T t, String msg) {
        this.r.setData(t);
        this.r.setCode(this.successCode);
        this.r.setMessage(msg);
        return this.r;
    }

    private R<T> setErrorMsg(Integer code) {
        this.r.setSuccess(false);
        this.r.setMessage(ErrorCode.getMessage(code));
        this.r.setCode(code);
        return this.r;
    }

    private R<T> setErrorMsg(Integer code, String msg) {
        this.r.setSuccess(false);
        this.r.setMessage(msg);
        this.r.setCode(code);
        return this.r;
    }

    public static <T> R<T> data(T t) {
        return (new ResultUtil()).setData(t);
    }

    public static <T> R<T> data(T t, String msg) {
        return (new ResultUtil()).setData(t, msg);
    }

    public static <T> R<T> success(String msg) {
        return (new ResultUtil()).setSuccessMsg(msg);
    }

    public static <T> R<T> error(Integer code) {
        return (new ResultUtil()).setErrorMsg(code);
    }

    public static String getMsg(int code) {
        return ErrorCode.getMessage(code);
    }

    public static <T> R<T> error(Integer code, String msg) {
        return (new ResultUtil()).setErrorMsg(code, msg);
    }

    public static <T> R<T> error(ErrorCode errorCode) {
        return (new ResultUtil()).setErrorMsg(errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> R<T> error(String msg) {
        return (new ResultUtil()).setErrorMsg(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }
}